/**
 * Copyright (C) 2011, mordredd666 [_at_] gmail.com
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *  
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *  
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package de.mordred.gui;

import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;

public class FragePanel extends JPanel
{

	private static final long serialVersionUID = -5380987712210197490L;
	JLabel _bild = new JLabel();
	JLabel _text = new JLabel();

	public FragePanel()
	{
		setLayout(new BorderLayout());
		add(_bild, BorderLayout.WEST);
		add(_text, BorderLayout.CENTER);
	}

	public FragePanel(String text)
	{
		this();
		setText(text);
	}

	public FragePanel(String text, Image image)
	{
		this(text, new ImageIcon(image));

	}

	public FragePanel(String text, final ImageIcon image)
	{
		this();
		setText(text);
		setImage(image);

	}

	/**
	 * Setzt den Text der Frabe
	 * 
	 * @param text
	 */
	public void setText(String text)
	{
		_text.setText("<html>" + text + "</html>");
		this.repaint();
	}

	public void setImage(final ImageIcon img)
	{
		if (img != null)
		{
			_bild.setIcon(new ImageIcon(img.getImage().getScaledInstance(50, 50, Image.SCALE_FAST)));

			for (MouseListener ml : _bild.getMouseListeners())
			{
				_bild.removeMouseListener(ml);
			}
			_bild.addMouseListener(new MouseAdapter()
			{
				@Override
				public void mouseClicked(MouseEvent e)
				{
					JPopupMenu jpop = FButton.getPopupWithPicture(img);
					jpop.setLocation((int) e.getLocationOnScreen().getX() - 10, (int) e
							.getLocationOnScreen().getY() - 10);
					jpop.setVisible(true);
				}

			});
			this.repaint();
		}
		else
			_bild.setIcon(null);
	}

}
